package com.kingdee.eas.custom.hr.attendance.app;

import java.sql.SQLException;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.common.DateUtils;
import com.kingdee.eas.custom.hr.attendance.CalendarTypeInfo;
import com.kingdee.eas.custom.hr.attendance.PersonScheduleInfo;
import com.kingdee.eas.custom.hr.attendance.WorkingTimeZoneMgrInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;

public class PersonScheduleControllerBean extends
		AbstractPersonScheduleControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.attendance.app.PersonScheduleControllerBean");

	@Override
	protected IObjectValue _getPersonScheduleByDate(Context ctx, String person,
			Date date) throws BOSException, EASBizException {
		PersonScheduleInfo personScheduleInfo = null;

		StringBuffer buffer = new StringBuffer();

		buffer.append("/*dialect*/SELECT A.FID AS FID,						\n");
		buffer.append("   	A.FPERSONID AS PERSONID,						\n");
		buffer.append("   	B.FID AS WORKTIMEZONEID,						\n");
		buffer.append("   	B.FNUMBER AS WORKTIMEZONENUMBER,				\n");
		buffer.append("   	B.FNAME_L2 AS WORKTIMEZONENAME,					\n");
		buffer.append("   	C.FID AS CALENDARTYPEID,						\n");
		buffer.append("   	C.FNUMBER AS CALENDARTYPENUMBER,				\n");
		buffer.append("   	C.FNAME_L2 AS CALENDARTYPENAME					\n");
		buffer.append("	FROM T_KQ_PERSONSCHEDULE A							\n");
		buffer.append("	LEFT JOIN CT_KQ_WORKINGTIMEZONEMGR B				\n");
		buffer.append(" ON A.FWORKTIMEZONEID = B.FID						\n");
		buffer.append("	LEFT JOIN CT_KQ_CALENDARTYPE C ON C.FID=A.FRESTID	\n");
		buffer.append(" WHERE TO_CHAR(A.FSCHEDULEDATE,'YYYY-MM-DD')=?		\n");
		buffer.append("		AND A.FPERSONID=?								\n");

		IRowSet rs = DbUtil.executeQuery(ctx, buffer.toString(), new Object[] {
				DateUtils.date2String(date, "yyyy-MM-dd"), person });

		try {

			while (rs.next()) {
				personScheduleInfo = new PersonScheduleInfo();
				personScheduleInfo.setId(BOSUuid.read(rs.getString("FID")));
				if (rs.getString("WORKTIMEZONEID") != null) {
					WorkingTimeZoneMgrInfo zone = new WorkingTimeZoneMgrInfo();
					zone.setId(BOSUuid.read(rs.getString("WORKTIMEZONEID")));
					zone.setNumber(rs.getString("WORKTIMEZONENUMBER"));
					zone.setName(rs.getString("WORKTIMEZONENAME"));
					personScheduleInfo.setWorkTimeZone(zone);
				}

				if (rs.getString("CALENDARTYPEID") != null) {
					CalendarTypeInfo type = new CalendarTypeInfo();
					type.setId(BOSUuid.read(rs.getString("CALENDARTYPEID")));
					type.setNumber(rs.getString("CALENDARTYPENUMBER"));
					type.setName(rs.getString("CALENDARTYPENAME"));
					personScheduleInfo.setRest(type);
				}
			}
		} catch (SQLException e) {
			throw new BOSException(e);
		}
		return personScheduleInfo;
	}
}